package com.ellricky.baselib.ui.cla

import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.LinearLayoutManager
import com.andr.common.http.callback.BaseCallback
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.adapter.cla.StuAddAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.StudentList
import com.ellricky.baselib.entity.student.StudentListEntity
import com.ellricky.baselib.http.Api.API_STUDENT_LIST
import com.ellricky.baselib.http.callback.EListCallback
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEListCallback
import kotlinx.android.synthetic.main.base_activity_person_add.*

class StuAddActivity : PersonAddActivity() {

    private lateinit var adapter: StuAddAdapter
    private var chooseEntity: StudentList? = null

    //57正式班58体验班59临时班
    private val classTypeId: String by lazy {
        intent.extras?.getString("class_type_dic_id") ?: ""
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = "添加学员"
        etSearch.hint = "搜索您要寻找的学员"
        initRv()
    }

    private fun initRv() {
        rvPerson.layoutManager = LinearLayoutManager(this)
        adapter = StuAddAdapter(this)
        adapter.setOnItemClickListener(this)
        rvPerson.adapter = adapter
    }

    override fun getUrlPath() = ""

    override fun getResponseCallback(): NewEBaseCallback {
        return object : NewEListCallback<StudentList>(this) {
            override fun onSuccess(data: MutableList<StudentList>?, msg: String?) {
                adapter.refresh(data ?: mutableListOf())
            }
        }
    }

    override fun label(): CharSequence = "添加学员"

    override fun addPersonParam(paramMap: HashMap<String, String>) {
        paramMap["action"] = "gzaAddStudent"
        paramMap["student_member_id"] = chooseEntity?.id ?: ""
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
    }

    override fun queryPersonParam(paramMap: HashMap<String, String>) {
        paramMap["action"] = getActions()
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        paramMap["eid"] = LocalCacheUtil.getUid()
    }

    private fun getActions(): String {
        return if (classTypeId == "58") {
            "saleStudentByInfo"
        } else {
            "gzaStudentByInfo"
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        adapter.choosePos = position
        adapter.notifyDataSetChanged()
        chooseEntity = adapter[position]
    }
}